package com.samsungmobile.tellafriend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.samsungmobile.common.Logger;
import com.samsungmobile.common.email.EmailCheck;
import com.samsungmobile.common.util.EasyString;
import com.samsungmobile.smCaptcha.servlet.Constants;

public class TellAFriend extends HttpServlet {
	
	//Parameters
	private String spSender_name = "";
	private String spSender_email = "";
	private String spReceiver_name = "";
	private String spReceiver_email = "";
	
	private String spTitle = "";
	private String spMessage = "";
	private String spCaptcha = "";
	private String spLang_cd = "";

	//Return Values
	private final String sfRTN_EMPTY_SENDER_NAME = "EMPTY_SENDER_NAME";
	private final String sfRTN_EMPTY_SENDER_EMAIL = "EMPTY_SENDER_EMAIL";
	private final String sfRTN_EMPTY_RECEIVER_NAME = "EMPTY_RECEIVER_NAME";
	private final String sfRTN_EMPTY_RECEIVER_EMAIL = "EMPTY_RECEIVER_EMAIL";
	
	private final String sfRTN_EMPTY_TITLE = "EMPTY_TITLE";
	private final String sfRTN_EMPTY_MESSAGE = "EMPTY_MESSAGE";
	private final String sfRTN_INVALID_SENDER = "INVALID_SENDER";
	private final String sfRTN_INVALID_RECEIVER = "INVALID_RECEIVER";
	
	private final String sfRTN_INVALID_CAPTCHA = "INVALID_CAPTCHA";
	private final String sfRTN_ERROR_SENDER = "ERROR_SENDER";
	private final String sfRTN_ERROR_RECEIVER = "ERROR_RECEIVER";
	private final String sfRTN_ERROR_TITLE = "ERROR_TITLE";
	
	private final String sfRTN_ERROR_SYSTEM = "ERROR_SYSTEM";
	private final String sfRTN_SUCCESS = "SUCCESS";

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession(false);
		
		HashMap hm = new HashMap();
		
		try{
			setParams(request);
			
			checkValidation(response, out);
			
			checkCpatcha(session, out);
			
			setMap(hm);
			
			sendMail(hm);
			
			out.println(sfRTN_SUCCESS);
			hm.put("RESULT", sfRTN_SUCCESS);
			
		}catch(Exception e){
			out.println(e.getMessage());
			hm.put("RESULT", e.getMessage());
		}finally{
			writeLog(hm);
		}

	}
	
	private void setMap(HashMap hm){
		
    	hm.put("TITLE", spTitle);
    	hm.put("SENDER_NAME", spSender_name);
    	hm.put("SENDER_EMAIL", spSender_email);
    	hm.put("RECEIVER_NAME", spReceiver_name);
    	hm.put("RECEIVER_EMAIL", spReceiver_email);
    	hm.put("MESSAGE", spMessage);
    	hm.put("LANG_CD", spLang_cd);		
	}
	
	private void setParams(HttpServletRequest request){
		
		spSender_name = EasyString.toString(request.getParameter("sender_name"));
		spSender_email = EasyString.toString(request.getParameter("sender_email"));
		spReceiver_name = EasyString.toString(request.getParameter("receiver_name"));
		spReceiver_email = EasyString.toString(request.getParameter("receiver_email"));
		
		spTitle = EasyString.toString(request.getParameter("title"));
		spMessage = EasyString.toString(request.getParameter("message"));
		spCaptcha = EasyString.toString(request.getParameter("captcha"));
		spLang_cd = EasyString.toString(request.getParameter("lang_cd"),"eng");
	}
	
	private void checkValidation(HttpServletResponse response, PrintWriter out) throws Exception{
	
		EmailCheck ec = new EmailCheck();
		
		if(spSender_name.equals("")){
			throw new Exception(sfRTN_EMPTY_SENDER_NAME);
		}else if(spSender_email.equals("")){
			throw new Exception(sfRTN_EMPTY_SENDER_EMAIL);
		}else if(spReceiver_name.equals("")){
			throw new Exception(sfRTN_EMPTY_RECEIVER_NAME);
		}else if(spReceiver_email.equals("")){
			throw new Exception(sfRTN_EMPTY_RECEIVER_EMAIL);
		}else if(spTitle.equals("")){
			throw new Exception(sfRTN_EMPTY_TITLE);
		}else if(spMessage.equals("")){
			throw new Exception(sfRTN_EMPTY_MESSAGE);
		}else if(!isEmail(spSender_email)){
			throw new Exception(sfRTN_INVALID_SENDER);
		}else if(!isEmail(spReceiver_email)){
			throw new Exception(sfRTN_INVALID_RECEIVER);
		}else if(spCaptcha.equals("")){
			throw new Exception(sfRTN_INVALID_CAPTCHA);
		}else if(!(ec.CheckEmailServer(spSender_email)) ){
			throw new Exception(sfRTN_ERROR_SENDER);
		}else if( !(ec.CheckEmailServer(spReceiver_email)) ){
			throw new Exception(sfRTN_ERROR_RECEIVER);
		}else if(spTitle.length() > 255){
			throw new Exception(sfRTN_ERROR_TITLE);
		}		

	}
	
	private void checkCpatcha(HttpSession session, PrintWriter out) throws Exception{
		
		String keyword_file= (String)session.getAttribute(Constants.SIMPLE_CAPCHA_SESSION_KEY) ;
		
		if(keyword_file==null || !spCaptcha.equals(keyword_file)){
			throw new Exception(sfRTN_INVALID_CAPTCHA);
		}
		
	}
	
	private void sendMail(HashMap hm) throws Exception{
		
		try{
			SendMail sm = new SendMail();
			sm.SendMail(hm);
		}catch(Exception e){
			throw new Exception(sfRTN_ERROR_SYSTEM);
		}
	}
	
	private void writeLog(HashMap hm){
		
		String sResult = (String)hm.get("RESULT");
		Logger.LOG_EMAIL.info("Mail Send Tell A Friend : "+sResult
						+"\tSender : "+spSender_email+"\tReceiver : "+spReceiver_email);
		
	}
	
	private static boolean isEmail(String sEmail) {
        if (sEmail==null) return false;
        boolean bRtn = Pattern.matches("[\\w\\~\\-\\.]+@[\\w\\~\\-]+(\\.[\\w\\~\\-]+)+", sEmail.trim());
        return bRtn;
    }
}
	